import java.sql.SQLOutput;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;

public class TestBinaryTreee {
    static class TreeNode{
        public char val;
        public  TreeNode left;
        public  TreeNode right;
        public TreeNode(char val) {
            this.val = val;
        }
    }
    public TreeNode creatTree(){
        TreeNode A  = new TreeNode('A');
        TreeNode B  = new TreeNode('B');
        TreeNode C  = new TreeNode('C');
        TreeNode D  = new TreeNode('D');
        TreeNode E  = new TreeNode('E');
        TreeNode F  = new TreeNode('F');
        TreeNode G  = new TreeNode('G');
        TreeNode H  = new TreeNode('H');
        A.left = B;
        A.right = C;
        B.left = D;
        B.right = E;
        C.left = F;
        C.right = G;
        E.right = H;
        return A;
        //就是根节点
    }
    // 前序遍历
    void preOrder(TreeNode root){
        if (root == null){
            return;
        }
        System.out.print(root.val+" ");
        //递归遍历左子树
        preOrder(root.left);
        //第归遍历右子树
        preOrder(root.right );


    }
    // 中序遍历
    void inOrder(TreeNode root){
        if (root == null){
            return;
        }
        inOrder(root.left);
        System.out.print(root.val+" ");
        inOrder(root.right);

    }
    // 后序遍历
    void postOrder(TreeNode root){
        if (root == null){
            return;
        }
        postOrder(root.left);
        postOrder(root.right);
        System.out.print(root.val+" ");

    }
    //求有多少个节点
    public int size(TreeNode root){
        if (root == null){
            return 0;
        }
        int ret = size(root.left)+
                size(root.right)+1;
        return ret;

    }
    public static int nodeSize;
    public void size2(TreeNode root){
        if (root == null){
            return ;
        }
        nodeSize++;
        size2(root.left);
        size2(root.right);
    }
    //求叶子结点个数
    public int getLeafNodeCount(TreeNode root){
        if (root == null){
            return 0;
        }
        if(root.left == null && root.right ==null){
            return 1;
        }
        return getLeafNodeCount(root.left)+
                getLeafNodeCount(root.right);
    }
    public int leafSize;
    public void getLeafNodeCount2(TreeNode root){
        if (root == null){
            return ;
        }
        if(root.left == null && root.right ==null){
           leafSize++;
        }
        getLeafNodeCount2(root.left);
        getLeafNodeCount2(root.right);
    }
    //获取第k层节点个数
    public int getKLevelNodeCount(TreeNode root,int k){
        if(root == null){
            return 0;
        }
        if (k==1){
            return 1;
        }
        return getKLevelNodeCount(root.left,k-1)+
                getKLevelNodeCount(root.right,k-1);
    }
    //获取二叉树高度
    public int getHeight(TreeNode root){
        if(root == null){
            return 0;
        }
        int leftHeight = getHeight(root.left);
        int rightHeight = getHeight(root.right);
        return leftHeight > rightHeight ?
                leftHeight+1 : rightHeight+1;

    }
    // 检测值为val的元素是否存在
    public TreeNode find(TreeNode root,char val){
        if(root == null){
            return null;
        }
        if (root.val == val ){
            return root;
        }
        TreeNode ret = find(root.left,val);
        if (ret != null){
            return ret;
        }
        ret = find(root.right,val);
            if (ret != null){
                return ret;
            }
        return null;
    }


    //层序遍历
    public void levelOrder(TreeNode root){
        Queue<TreeNode> queue = new LinkedList<>();
        if(root == null){
            return;
        }
        queue.offer(root);
        while (!queue.isEmpty()){
            TreeNode cur = queue.poll();
            System.out.print(cur.val+" ");
            if(cur.left != null){
                queue.offer(cur.left);
            }
            if (cur.right != null){
                queue.offer(cur.right);
            }
        }
        System.out.println();
    }
    // 判断一棵树是不是完全二叉树
   public boolean isCompleteTree(TreeNode root){
        Queue<TreeNode> queue = new LinkedList<>();
        if(root == null) return true;
        queue.offer(root);
        while (!queue.isEmpty()){
            TreeNode cur = queue.poll();
            if (cur == null){
                break;
            }
            queue.offer(cur.left);
            queue.offer(cur.right);
        }
        while (!queue.isEmpty()){
            TreeNode node =  queue.peek();
            if (node != null){
                return false;
            }else{
                queue.poll();
            }
        }
        return true;

   }
   //非递归的前序遍历
    public void preOrderNot(TreeNode root){
        Stack<TreeNode> stack = new Stack<>();
        TreeNode cur = root;

        while (cur != null || !stack.isEmpty()){
            while (cur != null){
                stack.push(cur);
                System.out.print(cur.val + " ");
                cur = cur.left;
            }
            TreeNode top = stack.pop();
            cur = top.right;
        }
    }
    //非递归的中序遍历
    public void inOrderNot(TreeNode root) {
        Stack<TreeNode> stack = new Stack<>();
        if(root==null){
            return;
        }
        TreeNode cur = root;
        while (cur != null || !stack.isEmpty()) {
            while (cur != null) {
                stack.push(cur);
                cur = cur.left;
            }
            TreeNode top = stack.pop();
            System.out.print(top.val + " ");
            cur = top.right;
        }
    }
    //非递归的后序遍历
    public void PostOrderNot(TreeNode root) {
        Stack<TreeNode> stack = new Stack<>();
        TreeNode cur = root;
        TreeNode prev = null;
        while (cur != null || !stack.isEmpty()) {
            while (cur != null) {
                stack.push(cur);
                cur = cur.left;
            }
            TreeNode top = stack.peek();
            if (top.right == null || top.right == prev) {
                //打印当前top 并弹出
                stack.pop();
                System.out.print(top.val + " ");
                prev = top;
            } else {
                cur = top.right;
            }
        }


    }
    void preOrder1(TreeNode root){
        if (root == null){
            return;
        }
        int e = 0;


        //递归遍历左子树
        if (root != null){
        preOrder1(root.left);
        e++;
            System.out.print(e);
        }

        //第归遍历右子树



    }







}
